//
//  HMDetailTableViewController.m
//  HMWeChat
//
//  Created by apple on 2017/5/11.
//  Copyright © 2017年 itheima. All rights reserved.
//

#import "HMDetailTableViewController.h"
#import "HMEditViewController.h"

@interface HMDetailTableViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *avartarImgV;
@property (weak, nonatomic) IBOutlet UILabel *nickNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *descLabel;

@end

@implementation HMDetailTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

- (void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];
    
    //获取当前账号的电子名片
    XMPPvCardTemp *myvCard = [HMXMPPManager sharedManager].xmppvCard.myvCardTemp;
    //设置电子名片信息
    self.nickNameLabel.text = myvCard.nickname;
    self.descLabel.text = myvCard.desc;
    self.avartarImgV.image = [UIImage imageWithData:myvCard.photo];
}


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

    HMEditViewController *editVc = segue.destinationViewController;
    
    if ([segue.identifier isEqualToString:@"desc"]) { //点击修改个性签名
        
        editVc.title = @"编辑个性签名";
        
    }else { //修改昵称
    
        editVc.title = @"编辑昵称";
    }
}

- (IBAction)clickAvatarImgV:(id)sender {
    
    //从相机中获取图片
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.delegate = self;
    //设置图片支持裁切
    picker.allowsEditing = YES;
    [self presentViewController:picker animated:YES completion:nil];
}


#pragma mark - UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    //获取编辑后的图片
    UIImage *img = info[UIImagePickerControllerEditedImage];
    
    //获取电子名片
    XMPPvCardTemp *myvCard = [HMXMPPManager sharedManager].xmppvCard.myvCardTemp;
    //设置头像
    myvCard.photo = UIImageJPEGRepresentation(img, 0.2);
    //更新电子名片
    [[HMXMPPManager sharedManager].xmppvCard updateMyvCardTemp:myvCard];
    
    [self dismissViewControllerAnimated:YES completion:nil];
}


@end
